package com.kintreda.ieco_server.module.api.web;

import com.kintreda.ieco_server.bean.system.ArtificialReply;
import com.kintreda.ieco_server.bean.system.KeyWord;
import com.kintreda.ieco_server.bean.variable.Dictionaries;
import com.kintreda.ieco_server.core.web.R;
import com.kintreda.ieco_server.module.account.service.IUserService;
import com.kintreda.ieco_server.module.autoreply.service.IArtificialReplyService;
import com.kintreda.ieco_server.module.autoreply.service.IKeyWordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Created by IntelliJ IDEA
 * @Project_Name ieco_server
 * @Package com.kintreda.ieco_server.module.autoreply.web
 * @Author ZhongYao_H
 * @Date 2019/5/21:19:17
 * @Year 2019 Month 05 Day 21
 * @Date_zh 2019 年 五月 星期二
 */
@Controller
@RequestMapping("/api/autorely")
public class ApiAutoreplyController {

    @Autowired
    IKeyWordService wordService;
    @Autowired
    IArtificialReplyService artificialReplyService;
    @Autowired
    IUserService userService;

    @RequestMapping(value = "/getkeyword",method = RequestMethod.POST)
    @ResponseBody
    public Object getKeyWord (String keyWord,String openid){
        keyWord = keyWord.toUpperCase();
        String key = keyWord.toUpperCase();
        R r = R.ok();

        if (Dictionaries.isexist(Dictionaries.MATCHINGA,keyWord)){
            return r.put("reply",wordService.getListByType("1","-2"));
        }

        if (Dictionaries.isexist(Dictionaries.MATCHINGB,keyWord)){
            return r.put("reply",wordService.getListByType("2","-2"));
        }

        if (Dictionaries.isexist(Dictionaries.MATCHINGC,keyWord)){
            //     return wordService.getListByType("3","-3");
        }

        /**
         * 类型  0  PET瓶 1 PE瓶 2 泡沫 3 硬质塑料 4 塑料袋膜 5 织物 6 玻璃
         * 7 复合 8 电子电器 9 书报  10 综合纸 11 金属 12 铝拉罐 13 黄纸板
         * -1 干垃圾 -3 湿垃圾 -4 有害垃圾
         */
        List<KeyWord> keyWordList = null;
        switch(key){
            case "PET瓶":
                 keyWordList = wordService.getListByTypeB("0");
                 break;
            case "PE瓶":
                keyWordList = wordService.getListByTypeB("1");
                break;
            case "泡沫":
                keyWordList = wordService.getListByTypeB("2");
                break;
            case "硬质塑料":
                keyWordList = wordService.getListByTypeB("3");
                break;
            case "塑料袋膜":
                keyWordList = wordService.getListByTypeB("4");
                break;
            case "织物":
                keyWordList = wordService.getListByTypeB("5");
                break;
            case "玻璃":
                keyWordList = wordService.getListByTypeB("6");
                break;
            case "复合":
                keyWordList = wordService.getListByTypeB("7");
                break;
            case "电子电器":
                keyWordList = wordService.getListByTypeB("8");
                break;
            case "书报":
                keyWordList = wordService.getListByTypeB("9");
                break;
            case "综合纸":
                keyWordList = wordService.getListByTypeB("10");
                break;
            case "金属":
                keyWordList = wordService.getListByTypeB("11");
                break;
            case "铝拉罐":
                keyWordList = wordService.getListByTypeB("12");
                break;
            case "黄纸板":
                keyWordList = wordService.getListByTypeB("13");
                break;
            case "干垃圾":
                keyWordList = wordService.getListByTypeB("-1");
                break;
            case "湿垃圾":
                keyWordList = wordService.getListByTypeB("-3");
                break;
            case "有害垃圾":
                keyWordList = wordService.getListByTypeB("-4");
                break;
            default:
                break;
        }
        if (keyWordList!=null){
            return R.ok().put("reply",keyWordList);
        }

        List<KeyWord> keyWordByKey = wordService.getKeyWordByKey(keyWord.toUpperCase());

        if (keyWordByKey.size()==0){
            String userId = userService.findUserByOpenId(openid).getId();
            ArtificialReply artificialReply = new ArtificialReply();
            artificialReply.setUserId(userId);
            artificialReply.setStatus("0");
            artificialReply.setBody(keyWord);
            artificialReplyService.save(artificialReply);
            return r.put("reply",keyWordByKey).put("code",1);
        }

        return r.put("reply",keyWordByKey);
    }


}
